using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Graphics;
using Java.IO;
using Android.Util;
using System.IO;
using Android.Media;

namespace TMM
{
    public static class Converter
    {
        public static string ConvertPictureTo64string(Bitmap picture, string filePath)
        {
            byte[] pictureBytes;
            String base64String;
            double picHeight = 250.0;
            double picWidth = 250.0;
            if (picture.Height > 250)
            {
                double ratio = 250.0 / (double)picture.Height;
                picWidth = ratio * (double)picture.Width;
            }
            else
                picWidth = picture.Width;
            if (picWidth > 250)
            {
                double ratio = 250.0 / picWidth;
                picHeight = ratio * picHeight;
                picWidth = 250.0;
            }
            picture = Bitmap.CreateScaledBitmap(picture, (int)picWidth, (int)picHeight, false);
            if (!string.IsNullOrWhiteSpace(filePath))
                picture = rotateBitmap(picture,filePath);

            MemoryStream baos = new MemoryStream();
            picture.Compress(Bitmap.CompressFormat.Jpeg, 100, baos);
            pictureBytes = ReadFully(baos);
            base64String = Base64.EncodeToString(pictureBytes, Android.Util.Base64Flags.Default);
            return base64String;
        }

        public static Bitmap rotateBitmap(Bitmap picture, String filePath)
        {
            ExifInterface exif = null;
            try
            {
                exif = new ExifInterface(filePath);
                int orientation = exif.GetAttributeInt(ExifInterface.TagOrientation, 1);
                int rotation = 0;
                if (orientation == 3)
                    rotation = 180;
                else if (orientation == 6)
                    rotation = 90;
                else if (orientation == 8)
                    rotation = 270;
                Matrix matrix = new Matrix();
                matrix.PostRotate(rotation);
                picture = Bitmap.CreateBitmap(picture, 0, 0, picture.Width, picture.Height, matrix, true);
            }
            catch (Exception)
            {
            }

            return picture;
        }

        public static byte[] ReadFully(System.IO.Stream input)
        {
            if (input == null)
                return null;

            input.Seek(0, SeekOrigin.Begin);
            byte[] buffer = new byte[input.Length + 1];
            using (MemoryStream ms = new MemoryStream())
            {
                int read;
                while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
                {
                    ms.Write(buffer, 0, read);
                }
                return ms.ToArray();
            }
        }
    }
}